package com.xzlcPT.controller;

import com.util.PageBean;
import com.xzlcPT.bean.*;
import com.xzlcPT.service.XzCompanyService;
import com.xzlcPT.service.XzPostionSendService;
import com.xzlcPT.service.XzPostionService;
import com.xzlcPT.service.XzResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Controller 层 LoginController
 *
 * @author 王天岳
 * @create 2017/3/30 17:43
 **/

@Controller
@RequestMapping("/")
@SessionAttributes({"userLogin", "resumeId"})
public class GoIndexController extends BaseController {

    @Autowired
    private XzResumeService xzResumeService;

    @Autowired
    private XzPostionService postionService;

    @Autowired
    private XzCompanyService companyService;

    @Autowired
    private XzPostionSendService postionSendService;

    //    跳转到主页
    @RequestMapping("/")
    public ModelAndView getIndex(HttpSession session) {
        ModelAndView mv = new ModelAndView();
        Map<String, Object> map = new HashMap<>();
        mv.setViewName("foreEnd3/index");
        PageBean<XzPostion> postionPageBean = postionService.selectByConditions(1, 6, null);
        PageBean<XzCompany> companyPageBean = companyService.selComCount(1, 12, map);
        mv.addObject("postion", postionPageBean.getList());
        mv.addObject("company", companyPageBean.getList());

        XzLogin userLogin = (XzLogin) session.getAttribute("userLogin");
        if (userLogin != null && userLogin.getLoginType() == 1) {
            mv.setViewName("foreEnd3/zpc_company_home");
            long companyId = userLogin.getCompany().getCompanyId();
            map.put("companyId", companyId);
            map.put("sendState", 0);
            List<XzResume> list0 = postionSendService.selByState(1, 6, map).getList().stream().map(XzPostionSend::getResumes).collect(Collectors.toList());
            map.put("sendState", 1);
            List<XzResume> list1 = postionSendService.selByState(1, 6, map).getList().stream().map(XzPostionSend::getResumes).collect(Collectors.toList());
            map.put("sendState", 2);
            List<XzResume> list2 = postionSendService.selByState(1, 6, map).getList().stream().map(XzPostionSend::getResumes).collect(Collectors.toList());
            mv.addObject("list0", list0);
            mv.addObject("list1", list1);
            mv.addObject("list2", list2);
        }
        return mv;
    }

    @RequestMapping("zp_index")
    public ModelAndView goZpIndex(@ModelAttribute("userLogin") XzLogin userLogin) {
        ModelAndView mv = new ModelAndView();
        if (userLogin.getLoginType() == 0) {
            mv.setViewName("foreEnd3/zp_index");
            XzResume resume = xzResumeService.selectByMemberId(userLogin.getMember().getMemberId());
            Integer completion = resume.getResumeCompletion();
            if (null != completion && completion >= 80) {

            } else {
                mv.setViewName("foreEnd3/zp_jianli");
                //简历完善程度低
            }
            mv.addObject("resumeId", resume.getResumeId());
            mv.addObject("resume", resume);
        } else {
            mv.setViewName("foreEnd3/zpc_company_home");
        }
        return mv;
    }

}